<?php
/* ******************************
 *	Recipie Production List aJax actions
 * ******************************
 */

include('../../../includes/classes/core.php');
$user_id	= $_SESSION['USERID'];
$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {
    case 'get_edit_page':
		$produsID	= $_REQUEST['id'];
		$barcode	= $_REQUEST['barcode'];
		$page 		= GetPage($produsID,$barcode);
	    $data			= array('page'	=> $page);


        break;
    case 'get_list':
	    $count		= $_REQUEST['count'];
	    $hidden		= $_REQUEST['hidden'];
	    $warehouseid    = $_REQUEST['id'];

	    $rsql = mysql_query("SELECT			warehouse_detail.id,
											warehouse_detail.barcode,
											`production`.`name`,
							CASE	WHEN	`warehouse_detail`.`status` = 0  THEN '<img src=\"media/images/delete1.png\">'
									WHEN	`warehouse_detail`.`status` = 1  THEN '<img src=\"media/images/apply.png\">'
	    					END AS ds
							FROM			warehouse_detail
							LEFT JOIN		`production` ON `production`.`id` =  `warehouse_detail`.`prod_id`
							WHERE			`warehouse_detail`.warehouse_id = '$warehouseid'  AND warehouse_detail.actived=1");

	    $data = array(
	    		"aaData"	=> array()
	    );


	    while ( $aRow = mysql_fetch_array( $rsql ) )
	    {
	    	$row = array();
	    	for ( $i = 0 ; $i < $count ; $i++ )
	    	{
	    		// General output
	    		$row[] = $aRow[$i];
	    	}
	    	$data['aaData'][] = $row;
	    }

        break;
    case 'end_cartridge_producing':
    	$warehouseDetid		= $_REQUEST['id'];
    	updateStatus($warehouseDetid);

        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;
echo json_encode($data);

function updateStatus($warehouseDetid){
	$result = mysql_fetch_assoc( mysql_query("	SELECT	`warehouse_detail`.`test_count` as `test_count`
												FROM	`warehouse_detail`
												WHERE	`warehouse_detail`.`id` = '$warehouseDetid' AND warehouse_detail.actived=1"));


/* 	$num_rows = mysql_num_rows(mysql_query("SELECT IF(warehouse_detail.type=2,warehouse_producing.warehouse_detail_id,warehouse_detail.id)
											FROM  warehouse_producing
											JOIN  warehouse_detail ON warehouse_detail.id=warehouse_producing.warehouse_detail_id
											WHERE warehouse_producing.warehouse_detail_id='$warehouseDetid'")); */

	if( $result['test_count'] > 0 /* && $num_rows > 0 */){
		mysql_query("	UPDATE		`warehouse_detail`
						SET 		`warehouse_detail`.`status`	= 1
						WHERE 		`warehouse_detail`.`id` 	= '$warehouseDetid'");
		mysql_query("	UPDATE		`warehouse_detail_history`
						SET 		`warehouse_detail_history`.`status`	= 1
						WHERE 		`warehouse_detail_history`.`id` 	= '$warehouseDetid'");

		$tester_id =	$_SESSION['USERID'];

		mysql_query("	UPDATE `warehouse_producing`
						SET     warehouse_producing.tester_id = $tester_id
						WHERE   warehouse_producing.warehouse_detail_id = '$warehouseDetid'");
		mysql_query("	UPDATE `warehouse_producing_history`
						SET     warehouse_producing_history.tester_id = $tester_id
						WHERE   warehouse_producing_history.warehouse_detail_id = '$warehouseDetid'");

		$rsql = mysql_fetch_array( mysql_query("SELECT	`warehouse_detail`.`barcode`
												FROM	`warehouse_detail`
												WHERE   `warehouse_detail`.`id` = '$warehouseDetid'"));

		$c_data = date('Y-m-d H:i:s');

		mysql_query("	UPDATE 	`reserve`
						SET		`reserve`.`production_condition`	=  1,
								`reserve`.`create_date`				= '$c_data'
						WHERE	`reserve`.`barcode`					= '$rsql[0]' ");
	}else{
		GLOBAL $error;
		$error = 'კატრიჯი ჯერ არ დატესტილა  ან არცერთი ნაწილი არ არის შეცვლილი!';
	}
}

function GetStatus($warehouseDetid){
	$data = '';
	$req = mysql_query("SELECT	`status`
						FROM 	`warehouse_detail`
						WHERE	`id` = '$warehouseDetid'");

	$array = array('არა','კი');
	for($i=1;$i<2;$i++){
		if(mysql_result($req ,0,'status') == "$i"){
			$data .= '<option value="' . $i . '" selected="selected">' . $array[$i] . '</option>';
		}else{
			$data .= '<option value="' . $i . '">' . $array[$i] . '</option>';
		}
	}
	return $data;
}

function GetPage($produsID,$barcode){
	if($barcode == ""){
		$rsql = mysql_query("SELECT		DISTINCT `warehouse_detail`.`barcode`,
										`production`.`name`,
										CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')'),
								      	CASE WHEN `warehouse_detail`.`status` = 0 THEN 'დაუსრულებელია'
								             WHEN `warehouse_detail`.`status` = 1 THEN 'დასრულებულია'
								        END AS ds,
										`persons`.`name` as `persons_name`
							FROM	`warehouse`
							LEFT JOIN ( `warehouse_detail` LEFT JOIN `production` ON `production`.`id` = `warehouse_detail`.`prod_id` )ON `warehouse_detail`.`warehouse_id` = `warehouse`.`id`
							LEFT JOIN ( `client_objects` LEFT JOIN `client` ON `client`.`id` = `client_objects`.`client_id` ) ON `client_objects`.`id` = `warehouse`.`client_object_id`
							LEFT JOIN	`warehouse_producing` ON `warehouse_producing`.`warehouse_detail_id` = `warehouse_detail`.`id`
							LEFT JOIN	`persons` ON `persons`.`id` = `warehouse_producing`.`person_id`
							WHERE `warehouse_detail`.`id` = '$produsID'
							LIMIT 1");
		$resQL		= mysql_fetch_array( $rsql );
	}else{
		$rsql = mysql_query("	SELECT		`warehouse_detail`.`barcode`,
											`production`.`name`,
											CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')'),
									      	CASE WHEN `warehouse_detail`.`status` = 0 THEN 'დაუსრულებელია'
									             WHEN `warehouse_detail`.`status` = 1 THEN 'დასრულებულია'
									        END AS ds,
											`warehouse_detail`.`id` AS `warehouse_detail_id`,
											`persons`.`name` as `persons_name`
								FROM	`warehouse`
								LEFT JOIN ( `warehouse_detail` LEFT JOIN `production` ON `production`.`id` = `warehouse_detail`.`prod_id` )ON `warehouse_detail`.`warehouse_id` = `warehouse`.`id`
								LEFT JOIN ( `client_objects` LEFT JOIN `client` ON `client`.`id` = `client_objects`.`client_id` ) ON `client_objects`.`id` = `warehouse`.`client_object_id`
								LEFT JOIN	`warehouse_producing` ON `warehouse_producing`.`warehouse_detail_id` = `warehouse_detail`.`id`
								LEFT JOIN	`persons` ON `persons`.`id` = `warehouse_producing`.`person_id`
								WHERE `warehouse_detail`.`barcode` = '".$barcode."' &&  `warehouse`.`id` = '$produsID'
								ORDER BY warehouse.id	DESC
								LIMIT 1");
		$resQL 		= mysql_fetch_array( $rsql );
		$produsID	= $resQL['warehouse_detail_id'];
	}

	if( $resQL[2] == ''){
		global $error;
		$error = 'შტრიხკოდი არასწორია';
		return ;
	}

	$data = '


		<div class="inner-table" >
			<input type="hidden" style="" name="search_id" class="search_init"  id="warehouseDetid" value="'.$produsID.'"/>
		</div>
		<div id="dialog-form">

			<div class="clear"></div>
			<fieldset>
			<legend>ძირითადი ინფორმაცია კატრიჯზე</legend>
				<table width="80%" class="dialog-form-table" cellpadding="10px" >
					<tr align="center" >
						<th>
							<label for="barcode">შტრიხკოდი : </label>'.$resQL[0].'
						</th>
					</tr>
					<tr align="center" >
						<th colspan="2">
							<label for="cartridge">კატრიჯი : </label>'.$resQL[1].'
						</th>
					</tr>
					<tr align="center">
						<th colspan="2">
							<label for="client_object">კლიენტი : </label>'.$resQL[2].'
						</th>
					</tr>
					<tr align="center">
						<th colspan="2">
							<label for="client_object">მდგომარეობა : </label>'.$resQL[3].'
						</th>
					</tr>
					<tr align="center">
						<th colspan="2">
							<label for="client_object">ხელოსანი : </label>'.$resQL['persons_name'].'
						</th>
					</tr>
				</table>
			</fieldset>
		<!-- ID -->
    </div>';
	return $data;

}
?>